package com.yolo.aim.yoloaim.util;

import ai.djl.modality.cv.Image;
import ai.djl.modality.cv.ImageFactory;
import org.bytedeco.javacpp.BytePointer;
import org.bytedeco.javacv.Java2DFrameConverter;
import org.bytedeco.javacv.OpenCVFrameConverter;
import org.bytedeco.opencv.global.opencv_highgui;
import org.bytedeco.opencv.opencv_core.Mat;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;


public class DrawRect {
    public static void main(String[] args) throws IOException {
        File file=new File("D:/p511088492.jpg");
        //Image img = ImageFactory.getInstance().fromFile(imageFile);
        BufferedImage bufImg= ImageIO.read(file);
        Image img = ImageFactory.getInstance().fromImage(bufImg);
        Mat mat=bufferedImage2Mat(bufImg);
        opencv_highgui.imshow("123",mat);
        opencv_highgui.waitKey();
    }
    public static Mat bufferedImage2Mat(BufferedImage original) {
        OpenCVFrameConverter.ToMat cv = new OpenCVFrameConverter.ToMat();
        return cv.convertToMat(new Java2DFrameConverter().convert(original));
    }
    public static int[] getRGB(Mat mat, int row, int clos) {
        //获取像素点
        BytePointer ptr = mat.ptr(row, clos);
        int channels = mat.channels();

        int[] rgb = new int[3];//注意这里是BGR不是RGB
        for (int i = 0; i < channels; i++) {
            //根据通道获取BGR
            byte pixel = ptr.get(i);
            rgb[i] = pixel;
        }
        return rgb;
    }
}
